// cSpell:words zxxx kechengjihua kecheng Cheng shouye Fengmian kechengzu
/* global $ ST window */

const {Page} = require("../Page")
const api = require("../api")

class IndexPage extends Page {
    pLoadData() {
        const q1 = api.main.get("zxxx/kechengjihua", {
            _pageSize: 4,
            home: "true"
        })
        const q2 = api.main.get("zxxx/kecheng", {
            _pageNo: 1, _pageSize: 4,
            type: "bzk", home: "true"
        })
        const q3 = api.main.get("zxxx/kecheng", {
            _pageNo: 1, _pageSize: 4,
            type: "hdk", home: "true"
        })

        return api.alertAjaxIfError(Promise.all([q1, q2, q3]))
            .then(([r1, r2, r3]) => {
                this.kechengjihuaList = r1.page
                this.kechengBzkList = r2.page
                this.kechengHdkList = r3.page
            })
    }

    pBuild() {
        const $page = $(ST.IndexPage()).appendTo(this.$pageParent)
        const $kechengzu = $page.find(".kechengzu-show")
            .css({top: "-20px"})
        const $currentIndicator = $page.mustFindOne(".current-indicator")

        this.onScroll = () => {
            // console.log(window.pageYOffset)
            $kechengzu.css({top: (-20 - 200 * window.pageYOffset / 1000) + "px"})
            this.styleSiteHeader()
        }

        if (this.kechengjihuaList && this.kechengjihuaList.length) {
            const $mainLessonList = $page.find(".main-lesson-list")
            const paneWidth = $mainLessonList.width() + "px"

            for (const kc of this.kechengjihuaList) {
                const $kc = $(ST.IndexPageMainLesson(kc))
                    .appendTo($mainLessonList)
                    .css({"left": "-" + paneWidth})
                const fm = kc.shouyeFengmian && kc.shouyeFengmian.path
                if (fm) $kc.css("background-image", `url(/r/${fm}`)

                $("<div>", {class: "indicator"}).appendTo($currentIndicator)
            }

            $mainLessonList.find(".main-lesson:first")
                .css({"left": "0"})
            $currentIndicator.find(".indicator:first").addClass("current")

            let current = 0
            this.kechengjihuaTimer = setInterval(() => {
                if (this.kechengjihuaList.length <= 1) return
                const last = current
                current = (current + 1) % this.kechengjihuaList.length
                $mainLessonList.find(`.main-lesson:eq(${last})`)
                    .css({"left": "0"})
                    .animate({"left": "-" + paneWidth}, 500)
                $mainLessonList.find(`.main-lesson:eq(${current})`)
                    .css({"left": paneWidth})
                    .animate({"left": "0"}, 500)

                $currentIndicator.find(".indicator").removeClass("current")
                $currentIndicator.find(`.indicator:eq(${current})`)
                    .addClass("current")
            }, 4000)
        }

        // 渲染标准课
        if (this.kechengBzkList && this.kechengBzkList.length) {
            const $list = $page.mustFindOne(".type-lesson-list.for-bzk")
            for (const kc of this.kechengBzkList) {
                $(ST.IndexPageKeCheng(kc)).appendTo($list)
            }
        }
        // 渲染互动课
        if (this.kechengHdkList && this.kechengHdkList.length) {
            const $list = $page.mustFindOne(".type-lesson-list.for-hdk")
            for (const kc of this.kechengHdkList) {
                $(ST.IndexPageKeCheng(kc)).appendTo($list)
            }
        }
    }

    styleSiteHeader() {
        if (window.pageYOffset >= 550) {
            $(".site-header").addClass("dark")
        } else {
            $(".site-header").removeClass("dark")
        }
    }

    open() {
        $("body").addClass("for-index")
        super.open()
        setTimeout(() => {
            this.styleSiteHeader()
        }, 200)

        $(window).on("scroll", this.onScroll)
    }

    close() {
        $(".site-header").removeClass("dark")
        $("body").removeClass("for-index")
        $(window).off("scroll", this.onScroll)
        if (this.kechengjihuaTimer) clearInterval(this.kechengjihuaTimer)
        super.close()
    }
}

exports.IndexPage = IndexPage
